package com.lqm.web.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @program: CrowdFunding
 * @description
 * @author: lqm
 * @create: 2018-12-03 21:48
 */

public class AdminInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //拦截的地址
        String url="http://" + request.getServerName() + ":"
                + request.getServerPort()
                + request.getRequestURI();
        Object user = request.getSession().getAttribute("admin_user");
        if (user!=null){
//            System.out.println("admin拦截了你，并且放行了");
            return true;
        }
        System.out.println("后台没有登录，被拦截【"+url+"】");
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("postHandle");
//        response.sendRedirect("/admin/index.html#/user/login");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
